var _page = $('.page');
var elem = document.getElementById('karma_photos');
var elem_con = elem.getElementsByClassName('box2')[0];
var photoSwipe = {
    /*用户信息数组*/
    imgArr: [],
    /*元素位置*/
    site: {
        _x_start: 0,
        _y_start: 0,
        _x_move: 0,
        _y_move: 0,
        _x_end: 0,
        _y_end: 0,
        top_val: 0,
        left_val: 0
    },
    /*当前下标*/
    index: bottlecms.localStorage.get("karma_index") || 1,
    touchon: 0,
    /*是否允许动画*/
    run: true,
    /*是否加载完成*/
    load: false,
    /*初始化*/
    init: function () {
        elem_con.innerHTML = this.imgHtml();
    },
    /*图片HTML*/
    imgHtml: function () {
        var index = this.index;
        // window.console.log(index);
        bottlecms.localStorage.set("karma_index", index);
        var str = "";
        bottlecms.get('/m/ajax/karma', {
            page: index
        }, function (data) {
            if (data.success == true) {
                str = data.list;
            } else {
                bottlecms.toast(data.msg);
            }
        }, null, {
            async: false
        });
        return karma_template(index, str);
    },
    /*移动动画*/
    animateMove: function (el, val) {
        if (!this.run) {
            return;
        }
        this.run = false;
        // window.console.log("" + doc_width * val + "px," + photoSwipe.top_val * 2.2 + "");
        /*CSS3动画方式*/
        el.style.transition = '0.3s';
        el.style.transform = "translate3d(" + doc_width * val + "px," + photoSwipe.top_val * 2.2 + "px,0px)";

        var moveTime = setTimeout(function () {
            el.remove();
            var ind_el = elem.getElementsByClassName("karma_ind-" + photoSwipe.index)[0];
            photoSwipe.activeEl(ind_el);
            photoSwipe.index++;
            elem_con.insertAdjacentHTML('afterBegin', photoSwipe.imgHtml());
            photoSwipe.run = true;
        }, 300);
    },
    /*复位动画*/
    animateReset: function (el) {
        el.style.transition = '0.3s';
        el.style.transform = "translate3d(0px,0px,0px)";

        var resetTime = setTimeout(function () {
            el.style.transition = '0s';
        }, 1000);
    },
    /*激活层*/
    activeEl: function (el) {
        el.style.zIndex = "2";
    },
    /*清除位置*/
    clearLocation: function () {
        this.left_val = 0;
    }
};
photoSwipe.init();

var doc_width = window.screen.width,
    doc_height = window.screen.height;

photoSwipe.activeEl(elem.getElementsByClassName("karma_ind-" + photoSwipe.index)[0]);
photoSwipe.index++;
elem_con.insertAdjacentHTML('afterBegin', photoSwipe.imgHtml());

//当手指触摸屏幕时候触发
elem.addEventListener('touchstart', function (e) {
    if (!photoSwipe.load || !photoSwipe.run) {
        return;
    }
    photoSwipe.touchon = 1;
    var ev = e || window.event;
    photoSwipe._x_start = ev.touches[0].pageX;
    photoSwipe._y_start = ev.touches[0].pageY;
    photoSwipe.top_val = 0;
    photoSwipe.left_val = 0;
}, false);

elem.addEventListener('touchmove', function (e) {
    e.preventDefault();
    if (!photoSwipe.load || !photoSwipe.run) {
        return;
    }
    photoSwipe.touchon = 2;
    var ev = e || window.event;
    photoSwipe._x_move = ev.touches[0].pageX;
    photoSwipe._y_move = ev.touches[0].pageY;

    photoSwipe.top_val = parseFloat(photoSwipe._y_move) - parseFloat(photoSwipe._y_start);
    photoSwipe.left_val = parseFloat(photoSwipe._x_move) - parseFloat(photoSwipe._x_start);

    var act_el = elem.getElementsByClassName("karma_ind-" + (photoSwipe.index - 1).toString(10))[0];

    act_el.style.transition = '0s';
    act_el.style.transform = "translate3d(" + photoSwipe.left_val + "px," + photoSwipe.top_val + "px,0px)";

}, false);

elem.addEventListener('touchend', function (e) {
    if (!photoSwipe.load || !photoSwipe.run || window.photoSwipeRun == false) {
        return;
    }
    var ev = e || window.event;
    photoSwipe._x_end = ev.changedTouches[0].pageX;
    photoSwipe._y_end = ev.changedTouches[0].pageY;
    var act_el = elem.getElementsByClassName("karma_ind-" + (photoSwipe.index - 1).toString(10))[0];
    //if(photoSwipe._x_start == photoSwipe._x_end && photoSwipe._y_start == photoSwipe._y_end) return;
    if (photoSwipe.touchon != 2) return;
    if (photoSwipe.left_val > 0 && photoSwipe.left_val > doc_width / 2 - doc_width / 4.5) {
        // window.console.log("右");
        photoSwipe.animateMove(act_el, 1);
    } else if (photoSwipe.left_val < 0 && photoSwipe.left_val < -doc_width / 2 + doc_width / 4.5) {
        // window.console.log("左");
        photoSwipe.animateMove(act_el, -1);
    } else if (photoSwipe.top_val > 0 && photoSwipe.top_val > doc_height / 2 - doc_height / 4.5) {
        // window.console.log("下");
        photoSwipe.animateReset(act_el);
    } else if (photoSwipe.top_val < 0 && photoSwipe.top_val < -doc_height / 2 + doc_height / 4.5) {
        // window.console.log("上");
        photoSwipe.animateReset(act_el);
    } else {
        // window.console.log("未滑动到位");
        photoSwipe.animateReset(act_el);
    }
    photoSwipe.touchon = 0;

}, false);


$(function () {
    photoSwipe.load = true;
});

//
$(_page).on("click", ".js-item, .buxihuan, .js-like", function (e) {
    e.preventDefault();
    e.stopPropagation();


    if ($(this).hasClass('buxihuan')) {
        photoSwipe.animateMove($(elem).find(".karma_ind-" + (photoSwipe.index - 1).toString(10))[0], -1);
        return;
    } else if ($(this).hasClass('js-like')) {
        var _this = this;
        like_fun($(this).data("id"), 1, function () {
            $(_this).addClass("on");
        })
        photoSwipe.animateMove($(elem).find(".karma_ind-" + (photoSwipe.index - 1).toString(10))[0], 1);
        return;
    }

    var id = $(this).attr('data-id');
    bottlecms.goto('/m/single/info/' + id);
})